import axios from "axios";
// 创建 axios 实例
const service = axios.create({
  baseURL: 'http://localhost:8000/api/',  // `${import.meta.env.VITE_APP_API_URL}${import.meta.env.VITE_APP_BASE_API}`,
  timeout: 50000,
  headers: { "Content-Type": "application/json;charset=utf-8" },
});

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    // 判断状态码是否是 200，如果是200，则返回 response.data
    if (response.status === 200) {
      return response.data
    }
    return response
  },
  (error) => {
    console.error('请求接口失败', error)
    return Promise.reject(error.message);
  }
);

// 导出 axios 实例
export default service;
